<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="文章名称" prop="articleTitle">
        <el-input
          v-model="queryParams.articleTitle"
          placeholder="请输入文章名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布时间">
        <el-date-picker
          v-model="dateValue"
          type="datetimerange"
          range-separator="-"
          start-placeholder="起始时间"
          end-placeholder="终止"
          value-format="yyyy-MM-dd HH:mm:ss"
          @change="setQueryDate"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="articleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="文章名称" align="center" prop="articleTitle" />
      <el-table-column label="分区" align="center">
        <template slot-scope="scope">
          <span v-if="zoningObj">{{ zoningObj[scope.row.zoningId] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发布用户" align="center" prop="customerName" />
      <el-table-column label="发布时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="点赞数" align="center" prop="likeCount" />
      <el-table-column label="状态" align="center" prop="hiddenName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="openDetail(scope.row)"
            v-hasPermi="['system:article:edit']"
          >查看</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleMsg(scope.row)"
            v-hasPermi="['system:article:edit']"
          >通知</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:article:edit']"
          >迁移</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="changeStatus(scope.row)"
            v-hasPermi="['system:article:remove']"
          >{{scope.row.hidden=='0'?'隐藏':'开放'}}</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="topOrCancelTop(scope.row)"
            v-hasPermi="['system:article:remove']"
          >{{scope.row.isTop=='0'?'置顶帖子':'取消置顶'}}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改社区文章对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="分区" prop="hidden">
          <el-select v-model="form.zoningId" placeholder="请选择分区">
            <el-option
              v-for="item in zoningList"
              :key="item.id"
              :value="item.id"
              :label="item.zoningNameCn"
            >{{ item.zoningNameCn }}</el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <Detail :detailVis.sync="detailVis" :detailObj="detailObj">
    </Detail>

    <!-- 回复对话框 -->
    <Dialogue
      v-if="dialogSession"
      v-model="dialogSession"
      :customer="customer"
     />
  </div>
</template>

<script>
import { listArticle, getArticle, delArticle, addArticle, updateArticle,listZoning,hideArticle,topOrCancelTop } from "@/api/system/article";
import Detail from './detail.vue';
import Dialogue from "../session/Dialogue.vue";

export default {
  name: "Article",
  components:{
    Detail,
    Dialogue
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 社区文章表格数据
      articleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        articleTitle: null,

      },
      zoningObj:{},
      zoningList:[],
      dateValue:[],
      // 表单参数
      form: {
        zoningId:''
      },
      // 表单校验
      rules: {},
      detailVis:false,
      detailObj:{},
      dialogSession: false,
      customer: {}
    };
  },
  created() {
    listZoning({pageNum: 1,pageSize:999,}).then(res=>{
      this.zoningList=res.rows
      res.rows.map((item)=>{
        this.zoningObj[item.id]=item.zoningNameCn
      })
    })
    this.getList();
  },
  methods: {
    handleMsg(row){
      this.customer = {name: row.customerName, id: row.customerId}
      this.dialogSession = true;
    },
    openDetail(row){
      getArticle(row.id).then(res=>{
        console.warn(res,'----')
        this.detailObj=res.data
        this.detailVis=true
      })
    },
    changeStatus(row){
      this.$confirm(`确定要${row.hidden=='0'?'开放':'隐藏'}吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          hideArticle({id:row.id}).then((res)=>{
            this.$message({
              type: 'success',
              message: '操作成功'
            });
            this.getList()
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消'
          });
        });
    },
    topOrCancelTop(row){
      this.$confirm(`确定要${row.isTop=='0'?'置顶帖子':'取消置顶'}吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        topOrCancelTop({id:row.id}).then((res)=>{
          this.$message({
            type: 'success',
            message: '操作成功'
          });
          this.getList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消'
        });
      });
    },
    setQueryDate(value){
      this.queryParams.createTimeEnd=new Date(value[1]).getTime()
      this.queryParams.createTimeStart=new Date(value[0]).getTime()
    },
    /** 查询社区文章列表 */
    getList() {
      this.loading = true;
      listArticle(this.queryParams).then(response => {
        this.articleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        zoningId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.createTimeStart=''
      this.queryParams.createTimeEnd=''
      this.dateValue=[]
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加社区文章";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form.zoningId=row.zoningId
      this.form.id=row.id
      this.open = true;
      this.title = "修改社区文章";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateArticle(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addArticle(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除？').then(function() {
        return delArticle(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/article/export', {
        ...this.queryParams
      }, `article_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
